package com.klikmee.sms.chat.web.usecases;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.clientactions.MessageBox;
import com.dotexe.webframework.core.datasources.CollectionDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.chat.web.dao.SignatureDAO;
import com.klikmee.sms.chat.web.util.SpringBeansLookup;
import com.klikmee.sms.services.chat.model.Signature;

public class SignatureListForm extends ClientApplication {

	private CollectionDataSource dsSignatures;
	private ArrayList<String> bcSignatures;
	
	private List<Signature> signatures;
	
	private SignatureDAO signatureDAO;
	
	private SignatureDetailsForm details;
	
	public void load() throws Exception {
		super.load();
		
		this.dsSignatures = new CollectionDataSource("dsSignatures");
		this.getDataSources().add(this.dsSignatures);
		this.bcSignatures = new ArrayList<String>();
		this.bcSignatures.add("Name");
		this.bcSignatures.add("Signature");
		this.bcSignatures.add("Active");

		this.triggers.add(new Trigger(this, "btnEditSignatureClick"));
		this.triggers.add(new Trigger(this, "btnAddSignatureClick"));
		this.triggers.add(new Trigger(this, "btnDeleteSignatureClick"));
		this.triggers.add(new Trigger(this, "gvSignaturesCellClick"));
		this.triggers.add(new Trigger(this, "gvSignaturesCellDoubleClick"));
	}

	@Override
	public void start() throws Exception {
		super.start();
		
		bindSignatureList();
	}

	public void btnEditSignatureClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		if (this.dsSignatures.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati potpis!");
			return;
		} else {
			details = createChildApplication(SignatureDetailsForm.class);
			details.setSignature((Signature) this.dsSignatures.getSelectedItem());
			details.triggers.get("btnSaveClick").addOnFire(this, "afterSignatureSave");
			details.setEditSignature(true);
			this.showChildApp(details);
			details.start();
		}
	}

	public void btnAddSignatureClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		details = createChildApplication(SignatureDetailsForm.class);
		Signature newSignature = new Signature();
		newSignature.setActive(true);
		details.setSignature(newSignature);
		details.triggers.get("btnSaveClick").addOnFire(this, "afterSignatureSave");
		details.setEditSignature(false);
		this.showChildApp(details);
		details.start();
	}

	public void btnDeleteSignatureClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		if (this.dsSignatures.getSelectedItem() == null) {
			MessageBox.Show(this, "Obavestenje", "Morate selektovati potpis!");
			return;
		} else {
			Signature sign = (Signature) this.dsSignatures.getSelectedItem();
			sign.setActive(false);
			signatureDAO.edit(sign);
			bindSignatureList();
		}
	}
	
	public void gvSignaturesCellClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
	}
	
	public void gvSignaturesCellDoubleClick() throws InstantiationException, IllegalAccessException, JSONException, Exception {
		details = createChildApplication(SignatureDetailsForm.class);
		details.setSignature((Signature) this.dsSignatures.getSelectedItem());
		details.triggers.get("btnSaveClick").addOnFire(this, "afterSignatureSave");
		details.setEditSignature(true);
		this.showChildApp(details);
		details.start();
	}
	
	public void afterSignatureSave() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		bindSignatureList();
	}

	private void bindSignatureList() throws SecurityException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, JSONException, NoSuchMethodException, InvocationTargetException {
		signatureDAO = SpringBeansLookup.getBean(SignatureDAO.class);
		signatures = signatureDAO.findAll();
		this.dsSignatures.setSource(signatures);
		this.dsSignatures.bind(bcSignatures);
	}
}
